package br.jonasLab.financistoweb.persistence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class BaseDAO{
	
	protected Connection cn;
	
	public BaseDAO(){
	}
	
	public Connection getConnection() throws SQLException{
		
		ResourceBundle jdbc = ResourceBundle.getBundle("br/jonasLab/financistoweb/bundle/jdbc");
		String url = jdbc.getString("url");
		String driverClassName = jdbc.getString("driverClassName");
		
	  	try {			
      		if(this.cn == null || this.cn.isClosed()){
      			Class.forName(driverClassName);      		
      			this.cn = DriverManager.getConnection(url);     
      		}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return this.cn;
	}
	
	public void closeConnection(){
		try {
			if (!this.cn.isClosed()) {
				this.cn.close();
			}
		} catch (SQLException e) {		
			e.printStackTrace();
		}
	}
	
	protected void finalize() throws Throwable {
		super.finalize();
		if (!cn.isClosed()) {
			cn.close();
		}
	}
}
